package org.example.outputformat;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.example.WordCountMap;
import org.example.WordCountReducer;

import java.net.URL;

/**
 * 固定写法
 */
public class WordCountDriver {
    public static void main(String[] args) {
        System.setProperty("hadoop.home.dir", "/opt/hadoop-3.3.6");

        URL path = WordCountDriver.class.getClassLoader().getResource("");
        try {
            Configuration configuration = new Configuration();
            configuration.set("dfs.permissions","false");
            Job job = Job.getInstance(configuration);

            // 设置jar
            job.setJarByClass(WordCountDriver.class);

            // 关联 map和reducer
            job.setMapperClass(LogMap.class);
            job.setReducerClass(LogReducer.class);

            // 设置map输出的kv类型
            job.setMapOutputKeyClass(Text.class);
            job.setOutputValueClass(NullWritable.class);

            // 设置最终输出的类型
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(NullWritable.class);

            job.setOutputFormatClass(LogOutputFormat.class);

            // 设置输入路径和输出路径
            FileInputFormat.setInputPaths(job, new Path("/Users/fengliulin/IdeaProjects/hadoop-learn/map-reduce/src/main/resources/wangzhan.txt"));
            FileOutputFormat.setOutputPath(job, new Path("/Users/fengliulin/Documents/Testsss/a12"));



            System.exit( job.waitForCompletion(true) ? 0 : 1);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

